<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>供应商统计</title>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
<link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

<script type="text/javascript" src="/static/js/date.js"></script>
<script type="text/javascript">
	
	

	
	function search(){
		$("#dg").datagrid("loadData",{total:0,rows:[]});
		$("#dg2").datagrid("loadData",{total:0,rows:[]});
		var s_begin=$("#s_begin").datebox("getValue");
		var s_end=$("#s_end").datebox("getValue");
		var state=$("#s_state").combobox("getValue");
		var supplierId;
		if(isNaN($("#s_supplier").combobox("getValue"))){
			supplierId="";
		}else{
			supplierId=$("#s_supplier").combobox("getValue");
		}
		
		$.post("/admin/purchaseList/list",{bPurchaseDate:s_begin,ePurchaseDate:s_end,state:state,'supplier.id':supplierId},function(result){
			var rows=result.rows;
			for(var i=0;i<rows.length;i++){
				var row=rows[i];
				$("#dg").datagrid('appendRow',{
					id:row.id,
					supplierName:row.supplier.name,
					date:row.purchaseDate,
					number:row.purchaseNumber,
					type:'进货单',
					amount:row.amountPayable,
					state:row.state,
					user:row.user.trueName,
					remarks:row.remarks
				});
			}
		},"json");
		
		$.post("/admin/returnList/list",{bReturnDate:s_begin,eReturnDate:s_end,state:state,'supplier.id':supplierId},function(result){
			var rows=result.rows;
			for(var i=0;i<rows.length;i++){
				var row=rows[i];
				$("#dg").datagrid('appendRow',{
					id:row.id,
					supplierName:row.supplier.name,
					date:row.returnDate,
					number:row.returnNumber,
					type:'退货单',
					amount:row.amountPayable,
					state:row.state,
					user:row.user.trueName,
					remarks:row.remarks
				});
			}
		},"json");
		
	}
	
	
	
	function formatAmount(val,row){
		return "¥"+val;
	}
	
	function formatState(val,row){
		if(val==1){
			return "已付";
		}else if(val==2){
			return "未付";
		}
	}
	
	
	
	function pay(){
		var selectedRows=$("#dg").datagrid("getSelections");
		if(selectedRows.length!=1){
			$.messager.alert("系统提示","请选择一条要结算的记录！");
			return;
		}
		var id=selectedRows[0].id;
		var type=selectedRows[0].type;
		$.messager.confirm("系统提示","您确定要结算吗？",function(r){
			if(r){
				if(type=="进货单"){
					$.post("/admin/purchaseList/update",{id:id},function(result){
						if(result.success){
							$.messager.alert("系统提示","提交成功！");
							search();
						}else{
							$.messager.alert("系统提示",result.errorInfo);
						}
					},"json");
				}else{
					$.post("/admin/returnList/update",{id:id},function(result){
						if(result.success){
							$.messager.alert("系统提示","提交成功！");
							search();
						}else{
							$.messager.alert("系统提示",result.errorInfo);
						}
					},"json");
				}
				
			}
		});
	}
	
	
	$(document).ready(function() {
		
		$("#s_begin").datebox("setValue",genLastMonthDayStr()); // 设置上个月日期
		$("#s_end").datebox("setValue",genTodayStr()); // 设置当前日期
		
		$("#dg").datagrid({
			onClickRow:function(index,row){
				if(row.type=="进货单"){
					$("#dg2").datagrid({
						url:'/admin/purchaseList/listGoods',
						queryParams:{
							purchaseListId:row.id
						}
					});
				}else if(row.type=="退货单"){
					$("#dg2").datagrid({
						url:'/admin/returnList/listGoods',
						queryParams:{
							returnListId:row.id
						}
					});
				}
			}
		});
		
	});
	
	
	function formatPrice(val,row){
		return "¥"+val;
	}
	
	function formatTotal(val,row){
		return "¥"+val;
	}
	

</script>
</head>
<body class="easyui-layout" style="margin: 1px">
 <div region="north" style="height: 350px">
	<table id="dg" title="供应统计" class="easyui-datagrid"
	fitColumns="true"  rownumbers="true" singleSelect="true"
	 fit="true" toolbar="#tb">
		<thead>
			<th field="id" width="20" align="center" hidden="true">ID</th>
			<th field="supplierName" width="60" align="center" >供应商</th>
			<th field="date" width="30" align="center">日期</th>
			<th field="number" width="40" align="center">单号</th>
			<th field="type" width="30" align="center">类型</th>
			<th field="amount" width="20" align="right" formatter="formatAmount">总金额</th>
			<th field="state" width="30" align="center" formatter="formatState">付款状态</th>
			<th field="user" width="20" align="center" >操作员</th>
			<th field="remarks" width="100" align="center">备注</th>
		</thead>
	</table>
	
	<div id="tb">
		<fieldset style="border-color: #E7F0FF">
			<legend>查询设置</legend>
			&nbsp;&nbsp;&nbsp;往来日期：&nbsp;
			<input id="s_begin" class="easyui-datebox" editable=false style="width:100px"/>
			&nbsp;&nbsp;-&nbsp;&nbsp;
			<input id="s_end" class="easyui-datebox" editable=false style="width:100px"/>
			&nbsp;&nbsp;&nbsp;供应商：&nbsp;<input class="easyui-combobox" id="s_supplier"  style="width: 200px" required="true" data-options="required:true,panelHeight:'auto',valueField:'id',textField:'name',url:'/admin/supplier/comboList'"/>
			&nbsp;&nbsp;&nbsp;是否付款&nbsp;
			<select class="easyui-combobox" id="s_state" style="width: 100px" editable="false" panelHeight="auto">
				<option value="">全部</option>
				<option value="1">已付</option>
				<option value="2">未付</option>
			</select>
			&nbsp;&nbsp;<a href="javascript:search()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
			&nbsp;&nbsp;<a href="javascript:pay()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">支付结算</a>
		</fieldset>
	</div>
 </div>
 <div region="center" style="margin-top: 5px">
     <table id="dg2"  class="easyui-datagrid"
		fitColumns="true"  rownumbers="true" singleSelect="true"
		 fit="true" >
			<thead>
				<th field="code" width="30" align="center">商品编码</th>
				<th field="name" width="150" align="center">商品名称</th>
				<th field="model" width="50" align="center">商品型号</th>
				<th field="price" width="50" align="right" formatter="formatPrice">单价</th>
				<th field="num" width="50" align="center">数量</th>
				<th field="unit" width="50" align="center">单位</th>
				<th field="total" width="50" align="right" formatter="formatTotal">总金额</th>
			</thead>
	</table>
   
 </div>	

</body>
</html>